/*

    This file is part of Blue Violin.

    Blue Violin is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Blue Violin is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero Public License for more details.

    You should have received a copy of the GNU Affero Public License
    along with Blue Violin.  If not, see <http://www.gnu.org/licenses/>.

    Blue Violin  Copyright (C) 2008  Blue Violin, Inc. & Josh Watts <josh.watts@blueviolin.org>
	For more information, visit http://www.blueviolin.org/

    This program comes with ABSOLUTELY NO WARRANTY.

*/

function BVLogin()
{
	this.m_fLoginHandler = $createDelegate(this, this.signIn);
	$get("btnMenuLogin").attachEvent("onclick", this.m_fLoginHandler);

	$get("btnMenuCreate").attachEvent("onclick",
									  $createDelegate(this, this.create));
}

BVLogin.prototype.signIn = function()
{
	// debugger;
	
	var txtUser = $get("txtUser");
	var txtPassword = $get("txtPassword");
	var oHttp = $http("POST", "scripts.php");
	var xmlRoot = null;
	var dtTemp = null;

	oHttp.send("action=login&email=" + encodeURIComponent(txtUser.value) + 
			   "&pwd=" + encodeURIComponent(txtPassword.value));
	xmlRoot = oHttp.responseXML;
	if ("false" == xmlRoot.text)
	{
		alert("I'm sorry - your login failed");
	}
	else
	{
		dtTemp = new Date(2015, 11, 31);
		document.cookie = "BLUEVIOLIN_ID=" + xmlRoot.text + ";expires=" + dtTemp.toGMTString() + ";path=/";
		
		this.userLoggedIn(txtUser.value);
		// $get("spnMenuLoginMessage").innerHTML = txtUser.value;
		// $get("spnCreateAccount").style.display = "none";
	}
	txtUser.value = "";
	txtPassword.value = "";
};


BVLogin.prototype.signOff = function()
{
	var dtTemp = new Date(1999,1,1);
	var oHttp = $http("GET", "scripts.php");
	document.cookie = "BLUEVIOLIN_ID=;expires=" + dtTemp.toGMTString() + ";path=/";
	oHttp.send("action=getnewlogin");
	this.noUser();
};


BVLogin.prototype.create = function()
{
	var oHttp = $http("POST", "scripts.php");
	var xmlRoot = null;
	var oUser = {"email":null, "password":null};

	window.showModalDialog("dialogs/createAccount.php",
						   oUser,
						   "dialogHeight: 150px;" +
						   "dialogWidth: 350px;" +
						   "center: yes;" +
						   "edge: raised;" + 
						   "scroll: no;" +
						   "status: no;" +
						   "unadorned: yes;");
	if (null == oUser.email &&
		null == oUser.password)
	{
		return;
	}
	oHttp.send("action=createlogin&email=" + encodeURIComponent(oUser.email) + 
			   "&pwd=" + encodeURIComponent(oUser.password));
	xmlRoot = oHttp.responseXML;

	if ("duplicateemail" == xmlRoot.text)
	{
		alert("A thousand apologies but that email address is already registered");
	}
	else if ("true" == xmlRoot.text)
	{
		this.userLoggedIn(oUser.email);
		alert("Thanks! If you have any problems feel free to contact me  - Josh Watts at josh.watts@blue-violin.com or 770-715-1334");
	}
};


BVLogin.prototype.userLoggedIn = function(sMessage)
{
	var btnMenuLogin = $get("btnMenuLogin");

	$get("spnMenuLoginMessage").innerHTML = sMessage;
	$get("spnCreateAccount").style.display = "none";
	$get("spnLoginAccount").style.display = "none";

	btnMenuLogin.detachEvent("onclick", this.m_fLoginHandler);
	this.m_fLoginHandler = $createDelegate(this, this.signOff);
	btnMenuLogin.attachEvent("onclick", this.m_fLoginHandler);
	btnMenuLogin.innerHTML = "Logoff";
	
};


BVLogin.prototype.noUser = function()
{
	var btnMenuLogin = $get("btnMenuLogin");
	
	if (null != this.m_fLoginHandler)
	{
		btnMenuLogin.detachEvent("onclick", this.m_fLoginHandler);
	}
	this.m_fLoginHandler = $createDelegate(this, this.signIn);
	btnMenuLogin.attachEvent("onclick", this.m_fLoginHandler);
	btnMenuLogin.innerHTML = "Login";
	
	$get("spnMenuLoginMessage").innerHTML = "";
	$get("spnCreateAccount").style.display = "inline";
	$get("spnLoginAccount").style.display = "inline";

};

	
